#pragma once

#include <iostream>
#include <pthread.h>
#include <vector>
#include <string>
#include <queue>
#include"Task.hpp"
struct ThreadInfo
{
    pthread_t tid;
    std::string threadname;
};

static const int defaultnum = 5;

template <class T>
class ThreadPool
{
public:
    // 封装
    // 上锁
    void Lock()
    {
        pthread_mutex_lock(&mutex);
    }
    // 解锁
    void Unlock()
    {
        pthread_mutex_unlock(&mutex);
    }
    // 唤醒
    void Wakeup()
    {
        pthread_cond_signal(&cond);
    }
    // 睡眠
    void ThreadSleep()
    {
        pthread_cond_wait(&cond, &mutex);
    }
    // 判断任务队列是否为空
    bool IsQueueEmpty()
    {
        return tasks.empty();
    }
    // 获取name
    std::string GetThreadname(pthread_t tid)
    {
        for (const auto &id : threads)
        {
            if (id.tid == tid)
            {
                return id.threadname;
            }
        }
        return "None";
    }

public:

    static void *handlerTask(void *args)
    {
        ThreadPool<T> *tp = static_cast<ThreadPool<T> *>(args);
        std::string name = tp->GetThreadname(pthread_self());
        while (true)
        {
            tp->Lock();
            while (tp->IsQueueEmpty())
            {
                tp->ThreadSleep();
            }
            T t = tp->Pop();
            tp->Unlock();
            t();
            std::cout << name << " run, "
                      << "result: " << t.Getresult() << std::endl;
        }
        return nullptr;
    }

    void Start()
    {
        int len = threads.size();
        for (int i = 0; i < len; i++)
        {
            threads[i].threadname = "Thread-" + std::to_string(i);
            pthread_create(&(threads[i].tid), nullptr, handlerTask, this /*threads[i].threadname*/);
        }
    }

    T Pop()
    {
        // 为什么这里看似不用加锁？
        T t = tasks.front();
        tasks.pop();
        return t;
    }

    T Push(const T &in)
    {
        Lock();
        tasks.push(in);
        Wakeup();
        Unlock();
    }

   static ThreadPool<T> *GetInstance()
    {
        if (_tp == nullptr)
        {
            pthread_mutex_lock(&lock);
            if (_tp == nullptr)
            {
                std::cout << "log: singleton create done first!" << std::endl;
                _tp = new ThreadPool<T>();
            }
            pthread_mutex_unlock(&lock);
        }
        return _tp;
    }

private:
    //若你自己写了，系统不会再去调它默认的了
    // ThreadPool()
    // {
    // }

    ThreadPool(int num = defaultnum)
        : threads(num)
    {
        pthread_mutex_init(&mutex, nullptr);
        pthread_cond_init(&cond, nullptr);
    }

    ~ThreadPool()
    {
        pthread_mutex_destroy(&mutex);
        pthread_cond_destroy(&cond);
    }
    ThreadPool(const ThreadPool<T>&)=delete;
    const ThreadPool<T>& operator=(const ThreadPool<T>&)=delete;
private:
    std::vector<ThreadInfo> threads; // 存储线程信息，管理线程池中的工作线程
    std::queue<T> tasks;             // 任务队列，存放待执行的任务
    pthread_mutex_t mutex;           // 互斥锁
    pthread_cond_t cond;             // 条件变量

    static ThreadPool<T> *_tp;
    //注意不是pthread_t***+不用加*指针形式
    static pthread_mutex_t lock;
};

template <class T>
ThreadPool<T> *ThreadPool<T>::_tp = nullptr;

template <class T>
pthread_mutex_t ThreadPool<T>::lock = PTHREAD_MUTEX_INITIALIZER;